"""
Theme management for presentations.
"""

import platform
from typing import Dict, Optional

from pptx.presentation import Presentation
from pptx.slide import Slide
from pptx.text.text import TextFrame

from .default_styles import DefaultStyles, ThemeColors


class ThemeManager:
    """Manages themes and styling for presentations."""
    
    def __init__(self, theme_name: str = "default"):
        self.theme_name = theme_name
        self.styles = DefaultStyles()
        self._current_theme_colors = self._get_theme_colors(theme_name)
    
    def _get_theme_colors(self, theme_name: str) -> Dict:
        """Get color scheme for specified theme."""
        theme_map = {
            "default": ThemeColors.BLUE_THEME,
            "blue": ThemeColors.BLUE_THEME,
            "dark": ThemeColors.DARK_THEME,
            "green": ThemeColors.GREEN_THEME,
            "red": ThemeColors.RED_THEME
        }
        return theme_map.get(theme_name, ThemeColors.BLUE_THEME)
    
    def get_font_name(self) -> str:
        """Get appropriate font name based on system platform."""
        system = platform.system()
        if system == "Windows":
            return self.styles.DEFAULT_FONT_NAME  # �o��
        elif system == "Darwin":  # macOS
            return "PingFang SC"  # macOS Chinese font
        else:  # Linux and others
            return self.styles.FALLBACK_FONT_NAME  # Arial
    
    def apply_theme(self, presentation: Presentation) -> None:
        """Apply theme to the entire presentation."""
        # Set slide size
        presentation.slide_width = self.styles.SLIDE_WIDTH
        presentation.slide_height = self.styles.SLIDE_HEIGHT
    
    def style_title_slide(self, slide: Slide, title_shape, subtitle_shape=None) -> None:
        """Apply styling to title slide elements."""
        if title_shape and hasattr(title_shape, 'text_frame'):
            self._style_text_frame(
                title_shape.text_frame,
                font_size=self.styles.TITLE_FONT_SIZE,
                font_bold=self.styles.TITLE_FONT_BOLD,
                alignment=self.styles.TITLE_ALIGNMENT,
                color=self._current_theme_colors['primary']
            )
        
        if subtitle_shape and hasattr(subtitle_shape, 'text_frame'):
            self._style_text_frame(
                subtitle_shape.text_frame,
                font_size=self.styles.BODY_FONT_SIZE,
                font_bold=False,
                alignment=self.styles.TITLE_ALIGNMENT,
                color=self._current_theme_colors['text']
            )
    
    def style_content_slide_title(self, title_shape) -> None:
        """Apply styling to content slide title."""
        if title_shape and hasattr(title_shape, 'text_frame'):
            self._style_text_frame(
                title_shape.text_frame,
                font_size=self.styles.SLIDE_TITLE_FONT_SIZE,
                font_bold=self.styles.SLIDE_TITLE_FONT_BOLD,
                alignment=self.styles.SLIDE_TITLE_ALIGNMENT,
                color=self._current_theme_colors['primary']
            )
    
    def style_body_text(self, text_shape) -> None:
        """Apply styling to body text."""
        if text_shape and hasattr(text_shape, 'text_frame'):
            self._style_text_frame(
                text_shape.text_frame,
                font_size=self.styles.BODY_FONT_SIZE,
                font_bold=self.styles.BODY_FONT_BOLD,
                alignment=self.styles.BODY_ALIGNMENT,
                color=self._current_theme_colors['text']
            )
    
    def style_bullet_list(self, text_shape) -> None:
        """Apply styling to bullet list."""
        if text_shape and hasattr(text_shape, 'text_frame'):
            text_frame = text_shape.text_frame
            self._style_text_frame(
                text_frame,
                font_size=self.styles.BULLET_FONT_SIZE,
                font_bold=self.styles.BULLET_FONT_BOLD,
                alignment=self.styles.BULLET_ALIGNMENT,
                color=self._current_theme_colors['text']
            )
            
            # Set bullet formatting for each paragraph
            for paragraph in text_frame.paragraphs:
                paragraph.level = 0  # Bullet level
    
    def _style_text_frame(self, text_frame: TextFrame, font_size, font_bold, 
                         alignment, color) -> None:
        """Apply common text styling to a text frame."""
        font_name = self.get_font_name()
        
        for paragraph in text_frame.paragraphs:
            paragraph.alignment = alignment
            
            for run in paragraph.runs:
                font = run.font
                font.name = font_name
                font.size = font_size
                font.bold = font_bold
                font.color.rgb = color
    
    def get_layout_dimensions(self) -> Dict:
        """Get layout dimensions and positioning."""
        return {
            'slide_width': self.styles.SLIDE_WIDTH,
            'slide_height': self.styles.SLIDE_HEIGHT,
            'title_top': self.styles.TITLE_TOP,
            'title_left': self.styles.TITLE_LEFT,
            'title_width': self.styles.TITLE_WIDTH,
            'title_height': self.styles.TITLE_HEIGHT,
            'slide_title_top': self.styles.SLIDE_TITLE_TOP,
            'slide_title_left': self.styles.SLIDE_TITLE_LEFT,
            'slide_title_width': self.styles.SLIDE_TITLE_WIDTH,
            'slide_title_height': self.styles.SLIDE_TITLE_HEIGHT,
            'content_top': self.styles.SLIDE_CONTENT_TOP,
            'content_left': self.styles.SLIDE_CONTENT_LEFT,
            'content_width': self.styles.SLIDE_CONTENT_WIDTH,
            'content_height': self.styles.SLIDE_CONTENT_HEIGHT,
            'margin_top': self.styles.CONTENT_MARGIN_TOP,
            'margin_left': self.styles.CONTENT_MARGIN_LEFT,
            'margin_right': self.styles.CONTENT_MARGIN_RIGHT,
            'margin_bottom': self.styles.CONTENT_MARGIN_BOTTOM,
        }
    
    def get_image_constraints(self) -> Dict:
        """Get image sizing constraints."""
        return {
            'max_width_ratio': self.styles.IMAGE_MAX_WIDTH_RATIO,
            'max_height_ratio': self.styles.IMAGE_MAX_HEIGHT_RATIO,
            'max_width': self.styles.SLIDE_WIDTH * self.styles.IMAGE_MAX_WIDTH_RATIO,
            'max_height': self.styles.SLIDE_HEIGHT * self.styles.IMAGE_MAX_HEIGHT_RATIO,
        }